package com.zibo.convalesce.modules.captcha;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 验证码接口
 *
 * @author lx
 */

@Controller
@RequestMapping("/captcha")
public class CaptchaController {

    private static final Logger logger = LoggerFactory.getLogger(CaptchaController.class);

    /**
     * 生成验证码
     *
     * @param response
     * @param request
     * @throws IOException
     */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpServletRequest request) throws Exception {
        logger.info("进入生成验证码接口...");
        ServletOutputStream outputStream = response.getOutputStream();
        // 动态英文和数字验证码
//        GifCaptcha captcha = new GifCaptcha(120, 40, 4);

        // 生成静态验证码
        SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
        // 设置类型为数字、字母混合
        captcha.setCharType(Captcha.TYPE_DEFAULT);
        // 设置内置字体
        captcha.setFont(Captcha.FONT_3);
        // 设置系统字体
        captcha.setFont(new Font("楷体", Font.BOLD, 28));
        String captCode = captcha.text();
        request.getSession().setAttribute("captCode", captCode);
        logger.info("考虑改成redis缓存形式，放到redis中，登录的时候做判断！！！，生成的验证码为{}",captCode);
        captcha.out(outputStream);
    }

}
